package com.sky.service.impl;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressMapper;
import com.sky.service.AddressService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class AddressServiceImpl implements AddressService {
    private final AddressMapper addressMapper;

    @Override
    public List<AddressBook> selectAddress() {
        List<AddressBook> addressBooks = addressMapper.selectAddress();
        return addressBooks;
    }

    @Override
    public void addAddress(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressMapper.addAddress(addressBook);
    }

    @Override
    public AddressBook selectDefaultAddress() {
        return addressMapper.selectDefaultAddress();
    }

    @Override
    public void updateAddress(AddressBook addressBook) {
        addressMapper.updateAddress(addressBook);
    }

    @Override
    public AddressBook selectAddressById(String id) {
        return addressMapper.selectAddressById(id);
    }

    @Override
    public void updateDefaultAddress(AddressBook addressBook) {
        addressMapper.updateDefault(0,BaseContext.getCurrentId());
        addressBook.setIsDefault(1);
        addressMapper.updateAddress(addressBook);
    }

    @Override
    public void deleteAddressById(String id) {
        addressMapper.deleteAddressById(id);
    }
}
